/*
 * KeyDrv.c
 *
 *  Created on: Apr 13, 2015
 *      Author: mxw
 */
#include "KeyDrv.h"
#include <S3C2440addr.h>
#include "LedDrv.h"

//key1 -> ENT1/GPF1  GPFCON[3:2]=0 :input
//key2 -> ENT4/GPF4  GPFCON[9:8]=0 :input
//key3 -> ENT2/GPF2  GPFCON[5:4]=0 :input
//key4 -> ENT0/GPF0  GPFCON[1:0]=0 :input

#define GPF1_4_2_0_MASK 0x033F
#define GPF1_4_2_0_AS_INT 0x022A
void init_key() {
    //step1:config key as interupt
    /*
     * MASK:
     *  00   00   00   11   00   11   11   11
     * GPF7 GPF6 GPF5 GPF4 GPF3 GPF2 GPF1 GPF0
     * 0x  0         3         3         F
     *
     * VAL:
     *  00   00   00   10   00   10   10   10
     * GPF7 GPF6 GPF5 GPF4 GPF3 GPF2 GPF1 GPF0
     * 0x  0         2         2         A
     *
     * */
    rGPFCON &= ~GPF1_4_2_0_MASK;
    rGPFCON |= GPF1_4_2_0_AS_INT;

    rEXTINT0 = 0x40444;
    rEINTMASK &= ~(1 << 4);

    /* rINTMSK:
     * ...  nBATT_FLT INT_CAM EINT8_23 EINT4_7 EINT3 EINT2 EINT1 EINT0
     *bit         7      6        5       4      3     2     1     0
     *mask_vlaue  0      0        0       1      0     1     1     1
     *mask_vlaue  0x17
     */
    rINTMSK &= ~(0x17);

    //step3: set interupt priority
    //config as default..
}


void key1_down() {
    set_led(LED_ALL, LED_OFF);//set all leds off
    set_led(LED_1, LED_ON); //led1 on
}

extern void new_muisc_go(unsigned char music_idx);
extern void request_to_stop();
unsigned char sing__i = 0;

void key2_down() {
    set_led(LED_ALL, LED_OFF);//set all leds off
    //set_led(LED_2, LED_ON); //led1 on
    new_muisc_go(sing__i);
    ++sing__i;
    if (3 <= sing__i) {
        sing__i = 0;
    }
}

void key3_down() {
    set_led(LED_ALL, LED_OFF);//set all leds off
    //set_led(LED_3, LED_ON); //led1 on
    request_to_stop();
}

void key4_down() {
    set_led(LED_ALL, LED_OFF);//set all leds off
    set_led(LED_4, LED_ON); //led1 on
}

